using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp.Data;
using Volo.Abp.Application.Dtos;
using Joy.Erp.BasicArchives.Permissions;
using Joy.Erp.BasicArchives.Core.Filters;

namespace Joy.Erp.BasicArchives.Procurements
{
    [Authorize]
    public class PurchaseOrderTypeAppService : BasicArchivesAppService, IPurchaseOrderTypeAppService
    {
        protected IPurchaseOrderTypeRepository PurchaseOrderTypeRepository => LazyServiceProvider.LazyGetRequiredService<IPurchaseOrderTypeRepository>();
        protected PurchaseOrderTypeManager PurchaseOrderTypeManager => LazyServiceProvider.LazyGetRequiredService<PurchaseOrderTypeManager>();

        [Authorize(BasicArchivesPermissions.PurchaseOrderType.Query)]
        public async Task<PurchaseOrderTypeDto> GetAsync(Guid id)
        {
            return ObjectMapper.Map<PurchaseOrderType, PurchaseOrderTypeDto>(
                await PurchaseOrderTypeRepository.GetAsync(id, true)
            );
        }

        [Authorize(BasicArchivesPermissions.PurchaseOrderType.Query)]
        public async Task<PurchaseOrderTypeDto> FindAsync(string filter)
        {
            var entity = await PurchaseOrderTypeRepository.FindAsync(x => x.DisplayName == filter);
            return ObjectMapper.Map<PurchaseOrderType, PurchaseOrderTypeDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.PurchaseOrderType.Query)]
        public async Task<PurchaseOrderTypeDto> FindBySourceKeyAsync(string sourceKey, string sourceType)
        {
            var entity = await PurchaseOrderTypeRepository.FindAsync(x => x.SourceKey == sourceKey && x.SourceType == sourceType);
            return ObjectMapper.Map<PurchaseOrderType, PurchaseOrderTypeDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.PurchaseOrderType.Query)]
        public async Task<PagedResultDto<PurchaseOrderTypeDto>> GetListAsync(FilterRequestDto input)
        {
            var totalCount = await PurchaseOrderTypeRepository.GetCountAsync(input.Filter, true);
            var list = await PurchaseOrderTypeRepository.GetPagedListAsync(input.Filter, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<PurchaseOrderTypeDto>(
                totalCount,
                ObjectMapper.Map<List<PurchaseOrderType>, List<PurchaseOrderTypeDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.PurchaseOrderType.Query)]
        public async Task<PagedResultDto<PurchaseOrderTypeDto>> QueryAsync(FilterGroupRequestDto input)
        {
            Expression<Func<PurchaseOrderType, bool>> expression = null;
            if (input != null && input.FilterGroup != null)
            {
                expression = FilterConverter.Convert<PurchaseOrderType>(input.FilterGroup);
            }

            var totalCount = await PurchaseOrderTypeRepository.GetCountAsync(expression, true);
            var list = await PurchaseOrderTypeRepository.GetPagedListAsync(expression, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<PurchaseOrderTypeDto>(
                totalCount,
                ObjectMapper.Map<List<PurchaseOrderType>, List<PurchaseOrderTypeDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.PurchaseOrderType.Create)]
        public async Task<PurchaseOrderTypeDto> CreateAsync(PurchaseOrderTypeCreateDto input)
        {
            var entity = ObjectMapper.Map<PurchaseOrderTypeCreateDto, PurchaseOrderType>(input);

            await PurchaseOrderTypeManager.CreateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.PurchaseOrderType.Update)]
        public async Task<PurchaseOrderTypeDto> UpdateAsync(Guid id, PurchaseOrderTypeUpdateDto input)
        {
            var entity = await PurchaseOrderTypeRepository.GetAsync(id);
            entity.SetConcurrencyStampIfNotNull(input.ConcurrencyStamp);

            ObjectMapper.Map(input, entity);

            await PurchaseOrderTypeManager.UpdateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.PurchaseOrderType.Delete)]
        public async Task DeleteAsync(Guid id)
        {
            await PurchaseOrderTypeManager.DeleteAsync(id);
        }
    }
}